﻿using CodeGenerator.DataAccess;
using CodeGenerator.Template;
using CodeGenerator.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace CodeGenerator.Impl
{
    public class GenerateImpl
    {
        static GenerateDataAccess generateDal = new GenerateDataAccess();

        public static void GenerateDbContext()
        {
            var tableList = generateDal.GetTableList();
            DbContextTemplate dbContextTemplate = new DbContextTemplate(tableList);
            String contentDbContext = dbContextTemplate.TransformText();
            System.IO.File.WriteAllText(Config.DataAccessPath + "\\" + Config.DBContextName, contentDbContext, Encoding.UTF8);
        }

        public static void GenerateDataAccess(string tableName)
        {
            var fieldList = generateDal.GetFieldList(tableName);
            var tableModel = generateDal.GetTableList().Single(o => o.Name == tableName);

            ModelTemplate modelTemplate = new ModelTemplate(fieldList, tableModel);
            String contentModel = modelTemplate.TransformText();
            System.IO.File.WriteAllText(Utility.FileHelper.PathExists(Config.BusinessEntitiesPath, tableName, Enums.GenerateType.Model), contentModel, Encoding.UTF8);

            DataAccessTemplate dataAccessTemplate = new DataAccessTemplate(fieldList, tableModel);
            String contentDataAccess = dataAccessTemplate.TransformText();
            System.IO.File.WriteAllText(Utility.FileHelper.PathExists(Config.DataAccessPath, tableName, Enums.GenerateType.DataAccess), contentDataAccess, Encoding.UTF8);

            LogicTemplate logicTemplate = new LogicTemplate(fieldList, tableModel);
            String contentLogic = logicTemplate.TransformText();
            System.IO.File.WriteAllText(Utility.FileHelper.PathExists(Config.BusinessLogicPath, tableName, Enums.GenerateType.Logic), contentLogic, Encoding.UTF8);

        }

        public static void GenerateModel(string tableName)
        {
            var fieldList = generateDal.GetFieldList(tableName);
            var tableModel = generateDal.GetTableList().Single(o => o.Name == tableName);

            ModelTemplate modelTemplate = new ModelTemplate(fieldList, tableModel);
            String contentModel = modelTemplate.TransformText();
            System.IO.File.WriteAllText(Utility.FileHelper.PathExists(Config.BusinessEntitiesPath, tableName, Enums.GenerateType.Model), contentModel, Encoding.UTF8);
        }

    }
}